<?php
/**
 * Import Interface : import a file
 * to initialize the data mapping 
 * 
 * @author Xavier VILAIN
 * @package Orx\Etl\Extractor
 * @license #license-orx-etl#
 * 
 */
use Orx\Etl\Extractor;
use Fox\Generator;
use Fox\Popup;
use Orx\Etl;
use Fox\Html\Input\File;
use Fox\Html\Block;
use Fox\Html\Screen;

$xview = Xview::instance();
$id = $xview->val('extractor');
$screen = new Screen('sDocument','import');
$block = new Block('bDocument');
// File Name
$block->add(new File('filename',true));
$screen->add($block)->noLegend();
if($screen->submitted())
{
	$fields = $screen->get();
	if(!$screen->errors())
	{
		try {
			$fileName = \Xview::moveFileToServer();
			// Load Extractor
			/* @var $extractor \Orx\Etl\Extractor*/
			$extractor = Fox\Dao::load(Extractor::className(),$id);
			$extractor->buildTemplate($fileName['destination']);
			$extractor->prepareUpdate(Extractor::CHANGE_IMPORT_TEMPLATE);
			if($extractor->update()) {
				Fox\Message::Success(Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_UPDATE,$extractor->label()));
			}
		} catch(Fox\Object\Exception\Save $e) {
			Fox\Message::Error($e);
		} catch(Exception $e) {
			Fox\Message::Error($e->getMessage());
		}
		echo $xview->messages(true,true);
		exit;
	}
}
$popup = new Popup('pImport',Msg(Etl::BUNDLE,Etl::IMPORT_TEMPLATE_FILE),$screen,500);
$screen->ajaxUpload(Generator::ajaxReload('MC_mapping'));
echo $popup->display();